<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%@ include file="/WEB-INF/views/shared/taglib.jsp"%>
<%
	request.setAttribute("modelConfigPath", "stock");
%>
<%@ include file="/WEB-INF/views/shared/master/contentHeader.jsp"%>
<style type="text/css">
	.template{display:none}
	td input{height:21px; width:98%;border:none;}
	td{height:21px;padding: 2px 2px !important;text-align:center }
</style>	
<t:panel cssClass="panel-flat form-detailed" customBody="true" customHead="true">
    <div class="detailed-navbar-content panel-body">
 
        <t:form modelAttribute="inStock" action="/purchase/purchase/saveOutStock" method="post" cssClass="form-horizontal">
            <div class="hide">
                <t:hidden path="inStockId"/>
                <t:hidden path="deleteMark"/>     
            </div>      
            <div class="detailed-navbar-content panel-body">
            	<t:formgroup >
	                <t:label path="purchaseId" cssClass="col-sm-1" />
	                <t:select2 path="purchaseId" allowClear="true" placeholder="请选择采购单"  colClass="col-xs-3" />
	                <t:label path="warehouse" cssClass="col-sm-1" />
	                <t:select2 path="warehouse" allowClear="true" placeholder="请选择仓库"  colClass="col-xs-3" />
	                <label   class="col-sm-1 control-label cursor-pointer text-right">供应商:</label>
	             
					<div class="col-sm-3">
						 <input readonly="true" class="form-control custom-input supplier" data-control-type="input" data-ts-forcestepdivisibility="none" type="text" value="" placeholder="请输入">
					
					</div>
	            </t:formgroup>	
            
  				<t:formgroup>
  					<t:label path="inStockNo" cssClass="col-sm-1" />
	                <t:input path="inStockNo" colClass="col-sm-3" />   
	              	<t:label path="createUserName" cssClass="col-sm-1" />
	                <t:input path="createUserName" colClass="col-sm-3" />
	                <t:label path="createDate" cssClass="col-sm-1" />
	                <t:datetime2  inputGroupIcon="icon-calendar" path="createDate" colClass="col-sm-3"></t:datetime2>
	            </t:formgroup>
	            <t:formgroup>
	            	
	                <t:label path="comment" cssClass="col-sm-1" />
	               	<t:input path="comment" colClass="col-sm-3" />     
	            </t:formgroup>
	  		</div>      
				<div class="table-responsive" style="height:268px">
				<table class="cntable table table-bordered dataTable " >	
					
					<thead>
						<tr class="success" >
							<th style="width:50px">		
							</th>
							<th style="width:70px">序号</th>
							<th style="width:150px">商品编码</th>
							<th style="width:120px">商品名称</th>
							<th style="width:110px">规格</th>
							<th style="width:80px">单位</th>
							<th style="width:120px">剩余可退数</th>
							<th style="width:100px">退货数量</th>
							<th style="width:120px">退货单价(元)</th>
							<th>小计(元)</th>
						</tr>
					</thead>
					<tbody >
						<tr>
							
							<td id="readfirst" colspan="10" align="center">请选这择采购单号</td>
							
						</tr>
						
						<tr class="template" style="height:40px">
							<td>
								<!-- <div class="col-md-6 col-sm-6"  onclick="addPurchaseCollocation()" ><i class="fa fa-plus-square"></i></div> -->
				            	<div class="col-md-6 col-sm-6" ><i class="fa fa-minus-square" data-for="delete"  onclick="delProduct(this)"></i></div>
							</td>
							<td>${status.index+1 }</td>
							<td  class="hide">
								<input  class="collocationIds"  id="collocationIds"  value="" type="hidden">
							</td>
							<td><label class="collocationNo"></label></td>
							<td><label class="cargoName"></label></td>
							<td><label class="propertyName"></label></td>
							<td><label class="unit"></label></td>
							<td class="checkInput"><label class="surplusNum"></label></td>
							<td class="num">
								<input onchange="checkInput(this)" class="form-control custom-input inStockNums "   type="number" value="0" placeholder="0">			               
							</td>						
							<td class="price">
								<input onchange="sumMoney(this)"  class="form-control custom-input inStockPrices" data-control-type="input" data-ts-forcestepdivisibility="none" type="number" value="0" placeholder="0">			               
							</td>
							<td class="sum">
								<input id="outStockNum" onchange="averageMoney(this)"  class="form-control custom-input outStockNum" data-control-type="input" data-ts-forcestepdivisibility="none" type="number" value="0" placeholder="0">			               
							</td>
						</tr>
						 <tr class="col-md-12 col-sm-12" id="addReference"></tr>
						
					</tbody>
					
				</table>     
				  
			</div>
			
          
        </t:form> 
    </div>
  
	 <t:panelFoot headingBtnCssClass="heading-btn pull-right-important">
		<t:button icon="icon-floppy-disk" onclick="saveData()" laddaType="progress" cssClass="btn-info" csize="sm">保存</t:button>
	</t:panelFoot>
</t:panel>

<script type="text/javascript">


$(function() {
	
	randomNumber();
	
	$("#purchaseId").change(function(){
		var purchaseId=$(this).find("option:selected").val();
		$("#readfirst").css("display","none")
	 	var url =$.getVirtualPath()+"/purchase/returnPurchase/purchaseSelect";
	    $.ajax({
	        type: 'get',
	        dataType: "json",
	        url: url,
	        data: "purchaseId="+purchaseId,
	        async: false,
	        success: function (result) {
	        	
	        	$(".product").remove();
				if(!$.isNull(result)){
					var purchaseCollocationsData=result.purchaseCollocations;
					$(".supplier").val(result.supplier.supplierName);
					purchaseCollocationsData.forEach(function(element,index){
					
						addPurchaseCollocation(element.collocation.collocationId,element.cargo.cargoNo,element.cargo.cargoName,element.collocation.propertyNameOne,element.collocation.propertyNameTwo,element.surplusNum)
					})
		             }
	        }
	    });
		 
		
	})
	
});
var puttyNum = 0;
function delProduct(element){
	console.log(element)
	$("."+$(element).attr("data-delete")).remove();
	console.log($("."+$(element).attr("data-delete")))
}
function addPurchaseCollocation(collocationId,cargoNo,cargoName,propertyNameOne,propertyNameTwo,surplusNum){
	puttyNum++;
	var divClass= "";	
	divClass = "product_"+puttyNum;
	var productInfo = $('.template').clone();
	productInfo.removeClass("template").addClass(divClass).addClass("product");
	productInfo.find("[data-for=delete]").attr("data-delete",divClass);
	productInfo.find(".collocationIds").addClass("select_collocationIds_"+puttyNum).attr("name","collocationIds");
	productInfo.find(".inStockNums").addClass("select_inStockNums_"+puttyNum).attr("name","inStockNums");
	productInfo.find(".inStockPrices").addClass("select_inStockPrices_"+puttyNum).attr("name","inStockPrices");
	
	productInfo.find(".collocationNo").addClass("add_collocationNo_"+puttyNum);
	productInfo.find(".cargoName").addClass("add_cargoName_"+puttyNum);
	productInfo.find(".propertyName").addClass("add_propertyName_"+puttyNum);
	productInfo.find(".surplusNum").addClass("add_surplusNum_"+puttyNum);
	productInfo.find(".unit").addClass("add_unit_"+puttyNum);
	
	$("#addReference").before(productInfo);
	if(!$.isNull(collocationId)){
		productInfo.find(".select_collocationIds_"+puttyNum).val(collocationId);
	}
	
	if(!$.isNull(collocationId)){
		productInfo.find(".add_collocationNo_"+puttyNum).text(cargoNo)
	}
	
	if(!$.isNull(cargoName)){
		productInfo.find(".add_cargoName_"+puttyNum).text(cargoName)
	}
	if(!$.isNull(propertyNameOne)&&!$.isNull(propertyNameTwo)){
		productInfo.find(".add_propertyName_"+puttyNum).text(propertyNameOne+"*"+propertyNameTwo)	
	}
	if(!$.isNull(propertyNameOne)&&$.isNull(propertyNameTwo)){
		productInfo.find(".add_propertyName_"+puttyNum).text(propertyNameOne)	
	}
	if($.isNull(propertyNameOne)&&!$.isNull(propertyNameTwo)){
		productInfo.find(".add_propertyName_"+puttyNum).text(propertyNameTwo)	
	}
	if(!$.isNull(surplusNum)){
		productInfo.find(".add_surplusNum_"+puttyNum).text(surplusNum)	
	}
	if(!$.isNull(surplusNum)){
		productInfo.find(".add_unit_"+puttyNum).text(surplusNum)	
	}
}

function checkInput(element){
	var surplusNum = $(element).parent().siblings(".checkInput").find("label").text();
	
	var prices=$(element).parent().siblings(".price").find("input").val();

	
	if(surplusNum<$(element).val()){
		$(element).val(surplusNum);
	}
	var sum=prices*$(element).val();
	$(element).parent().siblings(".sum").find("input").val(sum);
	
}

function sumMoney(element){
	var num=$(element).parent().siblings(".num").find("input").val();
	var sum=num*$(element).val();
	$(element).parent().siblings(".sum").find("input").val(sum);
}

function averageMoney(element){
	var num=$(element).parent().siblings(".num").find("input").val();
	
	
	if(num>0){
		var price=$(element).val()/num;
		
		$(element).parent().siblings(".price").find("input").val(price)
	}
}

function saveData(lBtn){
	var num=0;
	$(".inStockNums").each(function(){
		num=num+$(this).val();
		
	})
	
	if(num<=0){
		$.notify("无可退货物!!","warning");
		return;
	}
	if (!$("form:first").valid()){
		return;
	}
	lBtn.start();
	$("form:first").submit();
}
function printer(inStockId){


	top.openModal("/purchase/purchase/outStockDetailPrint?key="+inStockId,"inStock_form","采购退货单打印",1200,450);
}
function randomNumber(){
	const now =new Date();
	
	var  month=now.getMonth()+1;
	var day=now.getDate();
	var hour=now.getHours();
	var minutes=now.getMinutes();
	var seconds=now.getSeconds();
    var inStockNo="CG-R-"+ now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds
	$("#inStockNo").val(inStockNo);
	
}
</script>
<%@ include file="/WEB-INF/views/shared/base/scripts/jqueryui.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formtags.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formbuttons.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formpickers.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formselects.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formstyling.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/dialog.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/contentFooter.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/notifications.jsp"%>
